package com.wzcommon.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;


public class EnumConstraintValidator implements ConstraintValidator<EnumValidate, Integer> {

    //定义一个set集合,用于收集EnumValidate传入的values
    private Set<Integer> set = new HashSet<>();


    @Override
    public void initialize(EnumValidate constraintAnnotation) {

        //通过注解获取values
        int[] values = constraintAnnotation.values();
        for (int value : values) {
            set.add(value);
        }

    }
    //如果返回true表示验证成功-通过
    //返回false，表示验证失败-没有通过
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);

    }
}
